#! /bin/bash
#hdfs_to_ods.sh all/表名 [日期]
#1、判断参数是否传入
if [ $# -lt 1 ]
then
	echo "必须至少传入all/表名..."
	exit
fi
#2、判断日期是否传入，如果传入则加载指定日期数据，如果没有传入则加载前一天数据
[ "$2" ] && datestr=$2 || datestr=$(date -d '-1 day' +%F)

function load_data(){
	tableNames=$*
	sql="use medical;"
	for tableName in ${tableNames}
	do
		echo "/origin_data/medical/${tableName:4}/${datestr}"
		#判断数据目录是否存在，存在才加载数据
		hdfs dfs -test -e /origin_data/medical/${tableName:4}/${datestr}
		if [ $? -eq 0 ]
		then
			sql="${sql}load data inpath '/origin_data/medical/${tableName:4}/${datestr}' overwrite into table ${tableName} partition (dt='${datestr}');"
		fi
	done
	/opt/module/hive/bin/hive -e "${sql}"
}

#3、根据第一个参数加载数据
case $1 in
"all")
	load_data "ods_consultation_inc" "ods_dict_full" "ods_doctor_full" "ods_doctor_inc" "ods_hospital_full" "ods_medicine_full" "ods_patient_full" "ods_patient_inc" "ods_payment_inc" "ods_prescription_detail_inc" "ods_prescription_inc" "ods_user_full" "ods_user_inc"
;;
"ods_consultation_inc")
    load_data "ods_consultation_inc"
;;
"ods_dict_full")
    load_data "ods_dict_full"
;;
"ods_doctor_full")
    load_data "ods_doctor_full"
;;
"ods_doctor_inc")
    load_data "ods_doctor_inc"
;;
"ods_hospital_full")
    load_data "ods_hospital_full"
;;
"ods_medicine_full")
    load_data "ods_medicine_full"
;;
"ods_patient_full")
    load_data "ods_patient_full"
;;
"ods_patient_inc")
    load_data "ods_patient_inc"
;;
"ods_payment_inc")
    load_data "ods_payment_inc"
;;
"ods_prescription_detail_inc")
    load_data "ods_prescription_detail_inc"
;;
"ods_prescription_inc")
    load_data "ods_prescription_inc"
;;
"ods_user_full")
    load_data "ods_user_full"
;;
"ods_user_inc")
    load_data "ods_user_inc"
;;
*)
	echo "表名输入错误..."
;;
esac
